package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.qos.server.handler.QosProcessHandler;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Reference
    private IStrategyService strategyService;
    @Reference
    private IDestinationService destinationService;
    @Autowired
    private StrategyContentMapper strategyContentMapper;
    @Autowired
    private StrategyTagMapper strategyTagMapper;
    @Reference
    private IStrategyCommentService strategyCommentService;


    @Override
    public PageInfo<StrategyDetail> query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<StrategyDetail> list = strategyDetailMapper.selectForList(qo);
        return new PageInfo<StrategyDetail>(list);
    }

    /**
     * 吐丝专用
     * @param destinationId
     * @param toasts
     */

    public void getToasts(Long destinationId, ArrayList toasts) {
        Destination destination = destinationService.selecOne(destinationId);
        toasts.add(destination);
        if (destination.getParent() != null) {
            getToasts(destination.getParent().getId(), toasts);
        }
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {
        //好,首先进行一些残缺参数的补全
        //目的地id
        Destination dest = strategyService.get(strategyDetail.getStrategy().getId()).getDest();
        strategyDetail.setDest(dest);
        //summary
        //把文章截取100个字
        String content = strategyDetail.getStrategyContent().getContent();
        String summary=null;
        if (content.length()>100){

            summary =content.substring(0, 100);
        }else {
            summary=content;
        }
        strategyDetail.setSummary(summary);
        //是否国外
        ArrayList<Destination> totas = new ArrayList<>();
        getToasts(dest.getId(), totas);
        if (totas.get(totas.size() - 1).getId() != 1) {
            strategyDetail.setIsabroad(true);
        } else {
            strategyDetail.setIsabroad(false);
        }
        //接下来就是保存了
        //内容和明细分开操作
        //无论保存和修改都要配置文章ID
        StrategyContent strategyContent = strategyDetail.getStrategyContent();
        if (strategyDetail.getId() == null) {
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.insert(strategyContent);
        } else {
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            strategyContentMapper.updateByPrimaryKey(strategyContent);
        }
        //无论是否为空都要删除中间关系
        strategyDetailMapper.deleteTagRElationByDetailId(strategyDetail.getId());
        //接下来是标签了
        //先判断标签是否为空
        if (StringUtil.isNotEmpty(tags)){
        //不为空才进行保存
            String[] tagArr = tags.split(",");
            for (String tag : tagArr) {
                StrategyTag strategyTag = new StrategyTag();
                strategyTag.setName(tag);
                strategyTagMapper.insert(strategyTag);
                strategyDetailMapper.insertRelation(strategyDetail.getId(),strategyTag.getId());
            }
        }
    }

    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public void changeState(Long id, int state) {

    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<StrategyDetail> getDetailTop3(Long id) {
        return null;
    }

    @Override
    public List<StrategyDetail> selectDetailsByDestIdNumTop3(Long id) {
        return strategyDetailMapper.selectDetailTop3(id);
    }

    @Override
    public void addReplynumByDetailId(Long detailId) {
        //先增加数量
        strategyDetailMapper.addReplynumByDetailId(detailId);



    }

    @Override
    public void synchronizationStrategyStatis(StrategyDetail detail) {
        strategyDetailMapper.synchronizationStrategyStatis(detail);



    }

}
